package pchi.pchicamera.fragmenttabs;

import pchi.pchicamera.Apps;
import pchi.pchicamera.CompositeActivity;
import pchi.pchicamera.ImagePreview;
import pchi.pchicamera.R;
import pchi.pchicamera.data.SizeDevices;
import pchi.pchicamera.functions.ImageProcess;
import pchi.pchicamera.functions.OnTouchImage;
import pchi.pchicamera.helpers.BitmapHelpers;
import pchi.pchicamera.helpers.LayoutHelpers;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.Toast;

public class TabImagesPreview extends Fragment implements OnClickListener,
		OnTouchListener {

	private ImageView imagePreview;

	private ImageButton btnGoToComposite;
	private ImageButton btnSaveImage;
	private ImageButton btnClearAll;
	private ImageButton btnJobDone;
	private ImageButton btnTouchWithText;
	private ImageButton btnTouch;

	private TableLayout layoutColorControl;
	private TableLayout layoutTextControl;

	private ImageButton btnOkTouchWithText;
	private ImageButton btnCancelTouchWithText;
	private EditText editMyText;
	private EditText editMyTextSize;

	private ImageButton btnColorBlack;
	private ImageButton btnColorWhite;
	private ImageButton btnColorRed;
	private ImageButton btnColorGreen;
	private ImageButton btnColorBlue;
	private ImageButton btnColorBlueSea;
	private ImageButton btnColorPink;
	private ImageButton btnColorYellow;
	private ImageButton btnColorOrange;

	private String strText;
	private String strTextSize;

	private Bitmap originalBitmap;
	private Bitmap alteredBitmap;

	private int HEIGHT;
	private int WIDTH;

	private Bundle bundleGoToComposite = new Bundle();

	private int myTextColor = Color.GREEN;

	private int myBrushColor = Color.BLUE;

	private Boolean isTouch = false;
	private Boolean isTouchWithText = false;

	private String imagePathoriginalBitmap;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// to get screen width and hight
		HEIGHT = SizeDevices.getHeight(getActivity());
		WIDTH = SizeDevices.getWidth(getActivity());

		// setTag for tab ImagPreview
		Apps.getTagOfFragment().setTagOfTabImagesPreview(getTag());

		View root = inflater.inflate(R.layout.tab_image_preview, container,
				false);

		imagePreview = (ImageView) root.findViewById(R.id.ImageRoot);

		btnGoToComposite = (ImageButton) root.findViewById(R.id.btn_composite);
		btnGoToComposite.setOnClickListener(this);

		btnSaveImage = (ImageButton) root.findViewById(R.id.btn_save_image);
		btnSaveImage.setOnClickListener(this);

		btnClearAll = (ImageButton) root.findViewById(R.id.btn_clear_all);
		btnClearAll.setOnClickListener(this);

		btnJobDone = (ImageButton) root.findViewById(R.id.btn_job_done);
		btnJobDone.setOnClickListener(this);

		btnTouchWithText = (ImageButton) root
				.findViewById(R.id.btn_touch_with_text);
		btnTouchWithText.setOnClickListener(this);

		btnTouch = (ImageButton) root.findViewById(R.id.btn_touch);
		btnTouch.setOnClickListener(this);

		btnTouchWithText = (ImageButton) root
				.findViewById(R.id.btn_touch_with_text);
		btnTouchWithText.setOnClickListener(this);

		btnOkTouchWithText = (ImageButton) root
				.findViewById(R.id.ok_touch_with_text);
		btnOkTouchWithText.setOnClickListener(this);

		btnCancelTouchWithText = (ImageButton) root
				.findViewById(R.id.cancel_touch_with);
		btnCancelTouchWithText.setOnClickListener(this);

		editMyText = (EditText) root.findViewById(R.id.my_text);
		editMyTextSize = (EditText) root.findViewById(R.id.size);

		layoutColorControl = (TableLayout) root
				.findViewById(R.id.control_touch_with_color);
		layoutColorControl.setVisibility(View.INVISIBLE);

		layoutTextControl = (TableLayout) root
				.findViewById(R.id.control_touch_with_text);
		layoutTextControl.setVisibility(View.INVISIBLE);

		btnColorBlack = (ImageButton) root.findViewById(R.id.color_black);
		btnColorBlack.setOnClickListener(this);

		btnColorWhite = (ImageButton) root.findViewById(R.id.color_white);
		btnColorWhite.setOnClickListener(this);

		btnColorRed = (ImageButton) root.findViewById(R.id.color_red);
		btnColorRed.setOnClickListener(this);

		btnColorGreen = (ImageButton) root.findViewById(R.id.color_green);
		btnColorGreen.setOnClickListener(this);

		btnColorBlue = (ImageButton) root.findViewById(R.id.color_blue);
		btnColorBlue.setOnClickListener(this);

		btnColorBlueSea = (ImageButton) root.findViewById(R.id.color_blue_sea);
		btnColorBlueSea.setOnClickListener(this);

		btnColorPink = (ImageButton) root.findViewById(R.id.color_pink);
		btnColorPink.setOnClickListener(this);

		btnColorOrange = (ImageButton) root.findViewById(R.id.color_orange);
		btnColorOrange.setOnClickListener(this);

		btnColorYellow = (ImageButton) root.findViewById(R.id.color_yellow);
		btnColorYellow.setOnClickListener(this);

		// recived Image of mainActivity sent
		Intent i = getActivity().getIntent();
		Bundle bundle = i.getBundleExtra("BundleOfMain");
		imagePathoriginalBitmap = bundle.getString("imgPath");

		bundleGoToComposite.putString("imgComposite", imagePathoriginalBitmap);

		// decode file from image path
		originalBitmap = BitmapHelpers.decodeFile(imagePathoriginalBitmap,
				LayoutHelpers.getDeviceWidth(getActivity()), 0);
		// scale x,y
		// image root
		originalBitmap = Bitmap.createScaledBitmap(originalBitmap, WIDTH,
				HEIGHT, false);

		// image copy and use edit
		alteredBitmap = originalBitmap.copy(Bitmap.Config.RGB_565, true);

		// first set Image Preview use image root
		imagePreview.setImageBitmap(alteredBitmap);

		imagePreview.setOnTouchListener(this);

		return root;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_composite:
			Toast.makeText(getActivity(), "Go To Composite Image",
					Toast.LENGTH_SHORT).show();
			Intent intentGoToComposite = new Intent(getActivity(),
					CompositeActivity.class);
			intentGoToComposite.putExtra("imgComposite", bundleGoToComposite);
			startActivity(intentGoToComposite);
			break;
		case R.id.btn_save_image:
			Uri uriImageGotoComposite = OnTouchImage.saveImage(getActivity(),
					alteredBitmap);
			String path = ImagePreview.getRealPathFromURI(
					uriImageGotoComposite, getActivity());
			bundleGoToComposite.putString("imgPathComposite", path);
			break;
		case R.id.btn_clear_all:
			alteredBitmap = originalBitmap.copy(Config.RGB_565, true);
			imagePreview.setImageBitmap(alteredBitmap);
			layoutColorControl.setVisibility(View.INVISIBLE);
			layoutTextControl.setVisibility(View.INVISIBLE);
			break;
		case R.id.btn_job_done:
			// bundleGoToComposite.putString("imgComposite", );
			originalBitmap = alteredBitmap.copy(Config.RGB_565, true);
			isTouch = false;
			break;
		case R.id.btn_touch:
			isTouch = true;
			layoutColorControl.setVisibility(View.VISIBLE);
			layoutTextControl.setVisibility(View.INVISIBLE);
			break;
		case R.id.btn_touch_with_text:
			layoutTextControl.setVisibility(View.VISIBLE);
			layoutTextControl.setVisibility(View.VISIBLE);
			break;
		case R.id.ok_touch_with_text:
			isTouchWithText = true;
			strText = editMyText.getText().toString();
			strTextSize = editMyTextSize.getText().toString();
			break;
		case R.id.cancel_touch_with:
			isTouchWithText = false;
			layoutTextControl.setVisibility(View.INVISIBLE);
			break;
		case R.id.color_black:
			myTextColor = getActivity().getResources().getColor(R.color.black);
			myBrushColor = getActivity().getResources().getColor(R.color.black);
			break;
		case R.id.color_blue:
			myTextColor = getActivity().getResources().getColor(R.color.blue);
			myBrushColor = getActivity().getResources().getColor(R.color.blue);
			break;
		case R.id.color_blue_sea:
			myTextColor = getActivity().getResources().getColor(
					R.color.blue_sea);
			myBrushColor = getActivity().getResources().getColor(
					R.color.blue_sea);
			break;
		case R.id.color_green:
			myTextColor = getActivity().getResources().getColor(R.color.green);
			myBrushColor = getActivity().getResources().getColor(R.color.green);
			break;
		case R.id.color_orange:
			myTextColor = getActivity().getResources().getColor(R.color.orange);
			myBrushColor = getActivity().getResources()
					.getColor(R.color.orange);
			break;
		case R.id.color_pink:
			myTextColor = getActivity().getResources().getColor(R.color.pink);
			myBrushColor = getActivity().getResources().getColor(R.color.pink);
			break;
		case R.id.color_red:
			myTextColor = getActivity().getResources().getColor(R.color.red);
			myBrushColor = getActivity().getResources().getColor(R.color.red);
			break;
		case R.id.color_white:
			myTextColor = getActivity().getResources().getColor(R.color.whilte);
			myBrushColor = getActivity().getResources()
					.getColor(R.color.whilte);
			break;
		case R.id.color_yellow:
			myTextColor = getActivity().getResources().getColor(R.color.yellow);
			myBrushColor = getActivity().getResources()
					.getColor(R.color.yellow);
			break;
		default:
			break;
		}
	}

	float downx = 0;
	float downy = 0;
	float upx = 0;
	float upy = 0;

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		if (isTouch == true) {
			Canvas canvas = new Canvas(alteredBitmap);
			Paint paint = new Paint();
			Matrix matrix = new Matrix();
			paint.setColor(myBrushColor);
			paint.setStrokeWidth(5);
			canvas.drawBitmap(alteredBitmap, matrix, paint);

			int action = event.getAction();
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				downx = event.getX();
				downy = event.getY();
				break;
			case MotionEvent.ACTION_MOVE:
				upx = event.getX();
				upy = event.getY();

				canvas.drawLine(downx, downy, upx, upy, paint);

				imagePreview.invalidate();
				// imagePreview.setImageBitmap(alteredBitmap);
				downx = upx;
				downy = upy;
				break;
			case MotionEvent.ACTION_UP:
				upx = event.getX();
				upy = event.getY();

				canvas.drawLine(downx, downy, upx, upy, paint);

				imagePreview.invalidate();
				// imagePreview.setImageBitmap(alteredBitmap);
				break;
			case MotionEvent.ACTION_CANCEL:
				break;
			default:
				break;
			}
		}
		if (isTouchWithText == true) {
			isTouch = false;
			float scr_x = event.getRawX();
			float scr_y = event.getRawY();
			Paint paint = new Paint();
			paint.setStyle(Paint.Style.FILL);
			paint.setColor(myTextColor);
			Float f = Float.parseFloat(strTextSize);
			paint.setTextSize(f);
			Canvas canvas = new Canvas(alteredBitmap);
			int viewTop = getActivity().getWindow()
					.findViewById(Window.ID_ANDROID_CONTENT).getTop();
			scr_y = scr_y - viewTop;
			canvas.drawText(strText, scr_x, scr_y, paint);
			imagePreview.setImageBitmap(alteredBitmap);
		}
		return true;
	}

	public void updateView(int position) {
		switch (position) {
		case 0:
			alteredBitmap = ImageProcess.takeContrast(originalBitmap, 0.1);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 1:
			alteredBitmap = ImageProcess.takeContrast(originalBitmap, 10);
			imagePreview.setImageBitmap(alteredBitmap);
			break;

		case 2:
			alteredBitmap = ImageProcess.takeContrast(originalBitmap, 50);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 3:
			alteredBitmap = ImageProcess.takeContrast(originalBitmap, 100);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 4:
			alteredBitmap = ImageProcess.takeColorContrast(originalBitmap, 10);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 5:
			alteredBitmap = ImageProcess.takeColorContrast(originalBitmap, 50);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 6:
			alteredBitmap = ImageProcess.takeColorContrast(originalBitmap, 100);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 7:
			alteredBitmap = ImageProcess.SetBrightness(originalBitmap, 50);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 8:
			alteredBitmap = ImageProcess.SetBrightness(originalBitmap, 70);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 9:
			alteredBitmap = ImageProcess.SetBrightness(originalBitmap, 90);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 10:
			alteredBitmap = ImageProcess.SetBrightness(originalBitmap, -40);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 11:
			alteredBitmap = ImageProcess.SetBrightness(originalBitmap, -60);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 12:
			alteredBitmap = ImageProcess.SetBrightness(originalBitmap, -80);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 13:
			alteredBitmap = ImageProcess.boostColor(originalBitmap, 1, 30);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 14:
			alteredBitmap = ImageProcess.boostColor(originalBitmap, 2, 30);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 15:
			alteredBitmap = ImageProcess.boostColor(originalBitmap, 3, 30);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 16:
			alteredBitmap = ImageProcess.applyDarkFilter(originalBitmap);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 17:
			alteredBitmap = ImageProcess.doEmboss(originalBitmap);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 18:
			alteredBitmap = ImageProcess.doEngrave(originalBitmap);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 19:
			alteredBitmap = ImageProcess.applyFleaEffect(originalBitmap);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 20:
			alteredBitmap = ImageProcess
					.setGamma(originalBitmap, 0.5, 0.5, 0.5);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 21:
			alteredBitmap = ImageProcess
					.setGamma(originalBitmap, 0.6, 0.6, 0.6);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 22:
			alteredBitmap = ImageProcess
					.setGamma(originalBitmap, 0.8, 0.8, 0.8);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 23:
			alteredBitmap = ImageProcess
					.setGamma(originalBitmap, 1.0, 1.0, 1.0);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 24:
			alteredBitmap = ImageProcess
					.applyHueFilterEffect(originalBitmap, 1);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 25:
			alteredBitmap = ImageProcess
					.applyHueFilterEffect(originalBitmap, 2);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 26:
			alteredBitmap = ImageProcess
					.applyHueFilterEffect(originalBitmap, 6);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 27:
			alteredBitmap = ImageProcess.reduceColorDepth(originalBitmap, 16);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 28:
			alteredBitmap = ImageProcess.reduceColorDepth(originalBitmap, 32);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 29:
			alteredBitmap = ImageProcess.reduceColorDepth(originalBitmap, 64);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 30:
			alteredBitmap = ImageProcess.reduceColorDepth(originalBitmap, 128);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 31:
			alteredBitmap = ImageProcess.applySaturationFilter(originalBitmap,
					2);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 32:
			alteredBitmap = ImageProcess.applySaturationFilter(originalBitmap,
					3);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 33:
			alteredBitmap = ImageProcess.applySaturationFilter(originalBitmap,
					4);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 34:
			alteredBitmap = ImageProcess.applySaturationFilter(originalBitmap,
					5);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 35:
			alteredBitmap = ImageProcess.applySepiaToningEffect(originalBitmap,
					50, 2.2, 0, 0.2);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 36:
			alteredBitmap = ImageProcess.applySepiaToningEffect(originalBitmap,
					50, 1, 1, 3.3);
			imagePreview.setImageBitmap(alteredBitmap);
		case 37:
			alteredBitmap = ImageProcess.applySepiaToningEffect(originalBitmap,
					50, 2.2, 0, 2.2);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 38:
			alteredBitmap = ImageProcess.applySepiaToningEffect(originalBitmap,
					50, 2.2, 2.2, 0);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 39:
			alteredBitmap = ImageProcess.applyShadingFilter(originalBitmap,
					-15000);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 40:
			alteredBitmap = ImageProcess.applyShadingFilter(originalBitmap,
					-30000);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 41:
			alteredBitmap = ImageProcess.applyShadingFilter(originalBitmap,
					-105000);
			imagePreview.setImageBitmap(alteredBitmap);
		case 42:
			alteredBitmap = ImageProcess.applyShadingFilter(originalBitmap,
					-150000);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 43:
			alteredBitmap = ImageProcess.applySmoothEffect(originalBitmap, 5);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 44:
			alteredBitmap = ImageProcess.applySnowFallingEffect(originalBitmap);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 45:
			alteredBitmap = ImageProcess.applyTintEffect(originalBitmap, 30);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 46:
			alteredBitmap = ImageProcess.applyTintEffect(originalBitmap, 50);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		case 47:
			alteredBitmap = ImageProcess.applyTintEffect(originalBitmap, 80);
			imagePreview.setImageBitmap(alteredBitmap);
			break;
		default:
			break;
		}

	}

}
